import * as THREE from 'three';

class AnimationManager {
    constructor(target) {
        this.target = target;
        this.mixer = new THREE.AnimationMixer(target);
        this.animations = new Map();
        this.currentAction = null;
        this.fadeTime = 0.2;
        
        // 动画状态
        this.isTransitioning = false;
        this.queuedAnimation = null;
    }

    /**
     * 添加动画
     * @param {string} name 动画名称
     * @param {THREE.AnimationClip} clip 动画片段
     * @param {Object} options 动画选项
     */
    addAnimation(name, clip, options = {}) {
        const action = this.mixer.clipAction(clip);
        
        // 设置动画选项
        action.setLoop(options.loop || THREE.LoopRepeat);
        action.clampWhenFinished = options.clampWhenFinished || false;
        action.timeScale = options.timeScale || 1;
        
        this.animations.set(name, {
            action,
            options: {
                weight: options.weight || 1,
                fadeIn: options.fadeIn || this.fadeTime,
                fadeOut: options.fadeOut || this.fadeTime,
                ...options
            }
        });
    }

    /**
     * 播放动画
     * @param {string} name 动画名称
     * @param {Object} options 播放选项
     */
    play(name, options = {}) {
        if (!this.animations.has(name)) {
            console.warn(`Animation ${name} not found`);
            return;
        }

        const animation = this.animations.get(name);
        const { action, options: defaultOptions } = animation;
        const playOptions = { ...defaultOptions, ...options };

        // 如果正在过渡中且设置了队列
        if (this.isTransitioning && playOptions.queue) {
            this.queuedAnimation = { name, options: playOptions };
            return;
        }

        // 处理当前动画
        if (this.currentAction && this.currentAction !== action) {
            const fadeTime = playOptions.fadeOut;
            this.currentAction.fadeOut(fadeTime);
        }

        // 播放新动画
        action.reset();
        action.fadeIn(playOptions.fadeIn);
        action.play();

        // 设置权重和时间缩放
        action.setEffectiveWeight(playOptions.weight);
        action.setEffectiveTimeScale(playOptions.timeScale || 1);

        this.currentAction = action;
        
        // 处理一次性动画
        if (playOptions.oneShot) {
            this.isTransitioning = true;
            const duration = action._clip.duration;
            setTimeout(() => {
                this.isTransitioning = false;
                // 检查队列中的下一个动画
                if (this.queuedAnimation) {
                    const { name, options } = this.queuedAnimation;
                    this.queuedAnimation = null;
                    this.play(name, options);
                } else {
                    // 恢复默认动画
                    this.play(playOptions.returnTo || 'idle');
                }
            }, duration * 1000);
        }
    }

    /**
     * 更新动画
     * @param {number} deltaTime 时间增量
     */
    update(deltaTime) {
        if (this.mixer) {
            this.mixer.update(deltaTime);
        }
    }

    /**
     * 停止所有动画
     */
    stopAll() {
        this.animations.forEach(({ action }) => {
            action.stop();
        });
        this.currentAction = null;
    }

    /**
     * 清理资源
     */
    dispose() {
        this.stopAll();
        this.mixer.uncacheRoot(this.target);
        this.animations.clear();
    }
}

export { AnimationManager }; 